package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.constants.CommonConstants;
import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.HospitalPlaceMapper;
import net.zigin.nhi.could.waste.dao.SysUserHospitalMapper;
import net.zigin.nhi.could.waste.dto.HospitalPlaceDto;
import net.zigin.nhi.could.waste.exception.EpiCloudException;
import net.zigin.nhi.could.waste.model.HospitalPlace;
import net.zigin.nhi.could.waste.service.HospitalPlaceService;
import net.zigin.nhi.could.waste.util.CodeUtil;
import net.zigin.nhi.could.waste.util.EncryptDataUtil;
import net.zigin.nhi.could.waste.vo.HospitalPlaceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.HospitalPlaceMapper;
import net.zigin.nhi.could.waste.model.HospitalPlace;
import net.zigin.nhi.could.waste.service.HospitalPlaceService;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-8 0:01:02                                               
##################################################################*/


/**
 * HospitalPlace服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: HospitalPlaceServiceImpl
 * @date 2021-9-8 0:01:02
 */
@Service
public class HospitalPlaceServiceImpl extends AbstractServiceImpl<HospitalPlace, String> implements HospitalPlaceService {
    @Autowired
    private HospitalPlaceMapper hospitalPlaceMapper;
    @Autowired
    private SysUserHospitalMapper sysUserHospitalMapper;

    @Override
    public BaseMapper<HospitalPlace, String> getMapper() {
        return hospitalPlaceMapper;
    }

    @Override
    public String saveOrUpdate(HospitalPlaceDto hospitalPlaceDto) {
        AssertUtil.isNull(hospitalPlaceDto, "");
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        hospitalPlaceDto.setHospitalBaseId(sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId()));
        if (StringUtil.isNullOrEmpty(hospitalPlaceDto.getId())) {
            AssertUtil.isNull(hospitalPlaceDto.getName(), "");
            AssertUtil.isNull(hospitalPlaceDto.getCode(), "");
            AssertUtil.isNull(hospitalPlaceDto.getUserStaffId(), "");
            AssertUtil.isNull(hospitalPlaceDto.getHospitalDepartId(), "");
            AssertUtil.isNull(hospitalPlaceDto.getUserStaffId(), "");
            hospitalPlaceDto.setId(StringUtil.get32UUID());
            hospitalPlaceDto.setCreateUser(tokenInfo.getLoginName());
            hospitalPlaceDto.setCreateTime(new Date());
            hospitalPlaceDto.setModifyTime(new Date());
            HospitalPlace hospitalPlace = ReflectUtil.copyProperties(hospitalPlaceDto, HospitalPlace.class);
            hospitalPlaceMapper.insert(hospitalPlace);
            return hospitalPlace.getId();
        } else {
            hospitalPlaceDto.setModifyUser(tokenInfo.getLoginName());
            hospitalPlaceDto.setModifyTime(new Date());
            HospitalPlace hospitalPlace = ReflectUtil.copyProperties(hospitalPlaceDto, HospitalPlace.class);
            hospitalPlaceMapper.updateByPrimaryKeySelective(hospitalPlace);
            return hospitalPlace.getId();
        }
    }

    @Override
    public HospitalPlaceVo getInfoById(String id) {
        return hospitalPlaceMapper.getInfoById(id);
    }

    @Override
    public List<HospitalPlaceVo> getList(HospitalPlaceDto hospitalPlaceDto) {
        Map<String, Object> paramMap = getParam(hospitalPlaceDto);
        return hospitalPlaceMapper.getInfoPageByCondition(paramMap);
    }

    @Override
    public Integer getCount(HospitalPlaceDto hospitalPlaceDto) {
        Map<String, Object> paramMap = getParam(hospitalPlaceDto);
        return hospitalPlaceMapper.selectCount(paramMap);
    }

    private Map<String, Object> getParam(HospitalPlaceDto hospitalPlaceDto) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        hospitalPlaceDto.setHospitalBaseId(hospitalBaseId);
        return ParamsMapUtil.getInstance()
                .addParam("name", hospitalPlaceDto.getName())
                .addParam("code", hospitalPlaceDto.getCode())
                .addParam("hospitalDepartId", hospitalPlaceDto.getHospitalDepartId())
                .addParam("hospitalBaseId", hospitalPlaceDto.getHospitalBaseId())
                .addParam("userStaffId", hospitalPlaceDto.getUserStaffId())
                .addParam("pageSize", hospitalPlaceDto.getPageSize())
                .addParam("pageIndex", hospitalPlaceDto.getPageIndex());
    }

    @Override
    public void generateQrCode(String id, HttpServletResponse response) {
        HospitalPlace hospitalPlace = hospitalPlaceMapper.selectByPrimaryKey(id);
        if (hospitalPlace == null) {
            throw new EpiCloudException("暂存点不存在");
        }
        String encryptData = EncryptDataUtil.encryptData(CommonConstants.QRCODE_HOSPITAL_PLACE, hospitalPlace.getId());
        CodeUtil.encodeQrcode(hospitalPlace.getName(), encryptData, response);
    }

    @Override
    public void generateQrCode(List<String> ids, HttpServletResponse response) {
        List<Map<String, String>> encryptDataList = new ArrayList<>();
        for (String id : ids) {
            HospitalPlace hospitalPlace = hospitalPlaceMapper.selectByPrimaryKey(id);
            Map<String, String> map = new HashMap<>();
            map.put("name", hospitalPlace.getName());
            map.put("content", EncryptDataUtil.encryptData(CommonConstants.QRCODE_HOSPITAL_PLACE, id));
            encryptDataList.add(map);
        }
        CodeUtil.getCodeZip(response, encryptDataList);
    }
}